//
//  UIViewController+BarItem.m
//  duzhoumo
//
//  Created by dzmmac on 15/10/27.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "UIViewController+BarItem.h"
#import "NSUserDefaults+Addition.h"
#import "SVProgressHUD.h"
@implementation UIViewController (BarItem)


#pragma mark --------------------左侧返回按钮---------------------
/*
 * NavigationBar左侧返回按钮
 */
-(void)setLeftBtnItem{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIEdgeInsets inset   = UIEdgeInsetsMake(0, -15, 0, 0);
    leftButton.contentEdgeInsets = inset;
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    [leftButton setImage:[UIImage imageNamed:@"img_navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *left = [[UIBarButtonItem alloc ] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = left;
}


/**
 * @brief  选择城市按钮
 */
-(void)setCityBtnItem{
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIEdgeInsets inset = UIEdgeInsetsMake(0, -2, 0, 0);
    leftButton.contentEdgeInsets = inset;
    leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    NSString *city = [[NSUserDefaults standardUserDefaults] getUserSelectedCity];
    if (city.length >= 3) {
        city = [city substringToIndex:2];
        city = [NSString stringWithFormat:@"%@...",city];
        [leftButton setImageEdgeInsets:UIEdgeInsetsMake(1, 53, 0, 0)];
    }else{
        [leftButton setImageEdgeInsets:UIEdgeInsetsMake(1, 40, 0, 0)];
    }
    if (city == nil) {
        city = @"苏州";
    }
    [leftButton setTitle:city forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"img_index_cityarrow"] forState:UIControlStateNormal];
    leftButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    
    leftButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [leftButton.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
    leftButton.frame = CGRectMake(0, 0, 70, 35);
    
    [leftButton addTarget:self action:@selector(cityAction) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *left = [[UIBarButtonItem alloc ] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = left;
}

-(void)cityAction{
    
}

// 要获取返回的Action需重写该方法
-(void)backAction{
    [SVProgressHUD dismiss];
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 * @brief  左侧返回按钮
 */
-(void)setLeftCloseBtnItem{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIEdgeInsets inset = UIEdgeInsetsMake(0, -10, 0, 0);
    leftButton.contentEdgeInsets = inset;
    leftButton.frame = CGRectMake(0, 0, 30, 35);
    [leftButton setImage:[UIImage imageNamed:@"img_close"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *left = [[UIBarButtonItem alloc ] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = left;
}

-(void)closeAction{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark ---------------------自定义导航右侧按钮---------------------
/*
 * 自定义导航右侧按钮
 */
-(void)setCustomRightBtnItemWithImgName:(NSString *)imgName withTitle:(NSString *)title
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIEdgeInsets inset = UIEdgeInsetsMake(0, 0, 0, 0);
    leftButton.contentEdgeInsets = inset;
    
    if (imgName == nil){
        [leftButton setTitle:title forState:UIControlStateNormal];
        leftButton.titleLabel.textAlignment = NSTextAlignmentRight;
        leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [leftButton.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        leftButton.frame = CGRectMake(0, 0, 60, 35);
    }else{
        leftButton.frame = CGRectMake(0, 0, 30, 35);
        leftButton.imageEdgeInsets =  UIEdgeInsetsMake(0, 4, 0, 0);
        [leftButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    }
    [leftButton addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *left = [[UIBarButtonItem alloc ] initWithCustomView:leftButton];
    self.navigationItem.rightBarButtonItem = left;
}


/*
 * 自定义导航右侧按钮
 */
-(void)setCustomRightBtnItemWithImgName:(NSString *)imgName withTitle:(NSString *)title textColor:(UIColor *)textColor
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIEdgeInsets inset = UIEdgeInsetsMake(0, 0, 0, 0);
    leftButton.contentEdgeInsets = inset;
    
    if (imgName == nil){
        [leftButton setTitle:title forState:UIControlStateNormal];
        leftButton.titleLabel.textAlignment = NSTextAlignmentRight;
        leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [leftButton.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        leftButton.frame = CGRectMake(0, 0, 60, 35);
    }else{
        leftButton.frame = CGRectMake(0, 0, 30, 35);
        leftButton.imageEdgeInsets =  UIEdgeInsetsMake(0, 4, 0, 0);
        [leftButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    }
    
    [leftButton addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *left = [[UIBarButtonItem alloc ] initWithCustomView:leftButton];
    self.navigationItem.rightBarButtonItem = left;
}

-(void)rightItemAction{
    
}

@end
